<?php

namespace App\Http\Controllers\admin;

use App\Http\Requests;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use App\Models\Admin\Article;
use Cache, Event;

class ArticleController extends Controller
{
    protected $fields = [
        'user_id' => '',
        'cate_id' => '',
        'title' => '',
        'keywords'=>'',
        'description' => '',
        'content' => '',
        'html_content' => '',
    ];
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Article $art)
    {
        $article = $art->orderby('id','ASC')->select('article.*','admin_users.name as author','article_category.name as cate_name','tag.tag_name')
            ->join('admin_users','admin_users.id','=','article.user_id')
            ->leftJoin('article_category','article_category.id','=','article.cate_id')
            ->leftJoin('tag','tag.id','=','article.tag_id')
            ->Paginate(15);
        return view('Admin.article.index',compact('article'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $data = array();
        foreach($this->fields as $field => $default){
            $data[$field] = old($field,$default);
        }
        $uid = auth('admin')->user()->id;
        $tag_id = '';
        return view('admin.article.create',$data,compact('uid','tag_id'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request,Article $art)
    {
        foreach(array_keys($this->fields) as $field){
            $art->$field = $request->get($field);
        }
        if($request->input('tag_id')){
            $art->tag_id = $request->input('tag_id');
        }
        $art->save();
        event(new \App\Events\userActionEvent('\App\Models\Admin\Article', $art->id, 1, '发布文章' . $art->title));
        return redirect('/admin/article/index')->withSuccess('发布成功！');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Article $art,$id)
    {
        $article = $art->find((int)$id);
        $uid = auth('admin')->user()->id;
        return view('admin.article.edit',$article,compact('id','uid'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Article $art,$id)
    {
        $article = $art->find((int)$id);
        foreach(array_keys($this->fields) as $key => $field){
            $article->$field = $request->get($field);
        }
        if($request->input('tag_id')){
            $article->tag_id = $request->input('tag_id');
        }
        $article->save();
        event(new \App\Events\userActionEvent('\App\Models\Admin\Article', $article->id, 3, '编辑了文章' . $article->title));
        return redirect('/admin/article/index')->withSuccess('编辑成功！');

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Article $art,$id)
    {
        $article = $art->find((int)$id);
        $article->delete();
        return redirect()->back()
            ->withSuccess("删除成功");
    }
}
